/**
 * 
 */
package common;

import java.util.Date;

/**
 * Common logging utility.
 * 
 * @author chuxiaoyuan
 *
 */
public final class Logger {

	/**
	 * The debugging flag.
	 */
	private final boolean DEBUG = true;
	
	/**
	 * Create Logger instance.
	 */
	public Logger(Class<?> clazz) {
		this.setClazz(clazz);
	}

	/**
	 * The container class.
	 */
	private Class<?> clazz;

	/**
	 * @return the clazz
	 */
	private Class<?> getClazz() {
		return clazz;
	}

	/**
	 * @param clazz the clazz to set
	 */
	private void setClazz(Class<?> clazz) {
		this.clazz = clazz;
	}
	
	/**
	 * Logging levels.
	 * 
	 * @author chuxiaoyuan
	 *
	 */
	public enum LogLevel {
		log,		// Normal logging
		debug,		// Debug log
		error;		// Error log
	}
	
	/**
	 * Logging message.
	 * 
	 * @param level logging level
	 * @param msg the message to record.
	 * @param e the exception to log
	 */
	public void log(LogLevel level, String msg, Exception e) {
		
		String type = "";
		switch (level) {
			case log:
				type = "LOG";
				break;
				
			case debug:
				if (!DEBUG) {
					// Disable debug.
					return;
				}
				type = "DEBUG";
				break;
				
			case error:
				type = "ERROR";
				break;
				
			default:
				// No log.
				return;
		}
		
		// Log basic information.
		StringBuilder text = new StringBuilder();
		text.append("[").append(new Date()).append("] ");
		text.append("[").append(this.getClazz()).append("] ");
		text.append("[").append(type).append("] ");
		
		// Log message and exception
		text.append(msg);
		System.out.println(text);
		
		if (e != null) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Logging message.
	 * 
	 * @param msg the message to record.
	 * @param e the exception to log
	 */
	public void log(String msg, Exception e) {
		
		this.log(LogLevel.log, msg, e);
	}

	/**
	 * Logging message.
	 * 
	 * @param msg the message to record.
	 */
	public void log(String msg) {
		
		this.log(LogLevel.log, msg, null);
	}
	
	/**
	 * Logging debug message.
	 * 
	 * @param msg the message to record.
	 * @param e the exception to log
	 */
	public void debug(String msg, Exception e) {
		
		this.log(LogLevel.debug, msg, e);
	}

	/**
	 * Logging debug message.
	 * 
	 * @param msg the message to record.
	 */
	public void debug(String msg) {
		
		this.log(LogLevel.debug, msg, null);
	}
	
	/**
	 * Logging error message.
	 * 
	 * @param msg the message to record.
	 * @param e the exception to log
	 */
	public void error(String msg, Exception e) {
		
		this.log(LogLevel.error, msg, e);
	}

	/**
	 * Logging debug message.
	 * 
	 * @param msg the message to record.
	 */
	public void error(String msg) {
		
		this.log(LogLevel.error, msg, null);
	}
}
